package com.lz.virtualthread.common.utils;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: zip.li
 * @date: Created in 2023/4/21 9:05
 * @Description: cas锁实现
 */
public class CasLock {
    private AtomicReference<Thread> owner = new AtomicReference<>();

    public void lock() {
        Thread currentThread = Thread.currentThread();
        while (!owner.compareAndSet(null, currentThread)) {
            //循环等待
        }
    }

    public void unlock() {
        Thread currentThread = Thread.currentThread();
        owner.compareAndSet(currentThread, null);
    }
}
